package com.budwk.app.wxprogram.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.wxprogram.models.Wx_authorizationsuser;
import com.budwk.app.wxprogram.services.WxAuthorizationsuserService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@IocBean
@At("/platform/wxprogram/user")
@ApiDefinition(tag = "微信用户信息表")
@Slf4j
public class WxAuthorizationsuserController{
    @Inject
    private WxAuthorizationsuserService wxUserService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询微信用户信息表")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = Wx_authorizationsuser.class
    )
    @ApiResponses(
    implementation = Wx_authorizationsuser.class
    )
    @SaCheckPermission("wxprogram.user")
    public Result<?> list(@Param("..")Wx_authorizationsuser bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        
        if (bean.getNick_name() !=null && Strings.isNotBlank(bean.getNick_name().toString())) {
            cnd.and("nick_name","like","%"+bean.getNick_name()+"%");
        }
        
        if (bean.getSex() !=null && Strings.isNotBlank(bean.getSex().toString())) {
            cnd.and("sex","like","%"+bean.getSex()+"%");
        }
        
        if (bean.getPhone() !=null && Strings.isNotBlank(bean.getPhone().toString())) {
            cnd.and("phone","like","%"+bean.getPhone()+"%");
        }
        
        if (bean.getProvince() !=null && Strings.isNotBlank(bean.getProvince().toString())) {
            cnd.and("province","like","%"+bean.getProvince()+"%");
        }
        
        if (bean.getCity() !=null && Strings.isNotBlank(bean.getCity().toString())) {
            cnd.and("city","like","%"+bean.getCity()+"%");
        }
        
        if (bean.getCreate_time() !=null && Strings.isNotBlank(bean.getCreate_time().toString())) {
            Instant instant = bean.getCreate_time().toInstant();
            ZoneId zoneId = ZoneId.systemDefault();
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();

            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String startdateStr = localDateTime.format(fmt);
            String enddateStr = localDateTime.plusDays(1).format(fmt);
            cnd.and("create_time","between",new Object[]{startdateStr,enddateStr});        }
        
        if (bean.getHeadimg_url() !=null && Strings.isNotBlank(bean.getHeadimg_url().toString())) {
            cnd.and("headimg_url","like","%"+bean.getHeadimg_url()+"%");
        }
        
        if (bean.getCreate_id() !=null && Strings.isNotBlank(bean.getCreate_id().toString())) {
            cnd.and("create_id","like","%"+bean.getCreate_id()+"%");
        }
        
        if (bean.getUpdate_id() !=null && Strings.isNotBlank(bean.getUpdate_id().toString())) {
            cnd.and("update_id","like","%"+bean.getUpdate_id()+"%");
        }
        
        if (bean.getUpdate_time() !=null && Strings.isNotBlank(bean.getUpdate_time().toString())) {
            cnd.and("update_time","like","%"+bean.getUpdate_time()+"%");
        }
        
        if (bean.getRemark() !=null && Strings.isNotBlank(bean.getRemark().toString())) {
            cnd.and("remark","like","%"+bean.getRemark()+"%");
        }
        
        if (bean.getDel_flag() !=null && Strings.isNotBlank(bean.getDel_flag().toString())) {
            cnd.and("del_flag","like","%"+bean.getDel_flag()+"%");
        }
        
        if (bean.getApp_type() !=null && Strings.isNotBlank(bean.getApp_type().toString())) {
            cnd.and("app_type","like","%"+bean.getApp_type()+"%");
        }
        
        if (bean.getSubscribe() !=null && Strings.isNotBlank(bean.getSubscribe().toString())) {
            cnd.and("subscribe","like","%"+bean.getSubscribe()+"%");
        }
        
        if (bean.getSubscribe_scene() !=null && Strings.isNotBlank(bean.getSubscribe_scene().toString())) {
            cnd.and("subscribe_scene","like","%"+bean.getSubscribe_scene()+"%");
        }
        
        if (bean.getSubscribe_time() !=null && Strings.isNotBlank(bean.getSubscribe_time().toString())) {
            cnd.and("subscribe_time","like","%"+bean.getSubscribe_time()+"%");
        }
        
        if (bean.getSubscribe_num() !=null && Strings.isNotBlank(bean.getSubscribe_num().toString())) {
            cnd.and("subscribe_num","like","%"+bean.getSubscribe_num()+"%");
        }
        
        if (bean.getCancel_subscribe_time() !=null && Strings.isNotBlank(bean.getCancel_subscribe_time().toString())) {
            cnd.and("cancel_subscribe_time","like","%"+bean.getCancel_subscribe_time()+"%");
        }
        
        if (bean.getOpen_id() !=null && Strings.isNotBlank(bean.getOpen_id().toString())) {
            cnd.and("open_id","like","%"+bean.getOpen_id()+"%");
        }
        
        if (bean.getCountry() !=null && Strings.isNotBlank(bean.getCountry().toString())) {
            cnd.and("country","like","%"+bean.getCountry()+"%");
        }
        
        if (bean.getLanguage() !=null && Strings.isNotBlank(bean.getLanguage().toString())) {
            cnd.and("language","like","%"+bean.getLanguage()+"%");
        }
        
        if (bean.getUnion_id() !=null && Strings.isNotBlank(bean.getUnion_id().toString())) {
            cnd.and("union_id","like","%"+bean.getUnion_id()+"%");
        }
        
        if (bean.getGroup_id() !=null && Strings.isNotBlank(bean.getGroup_id().toString())) {
            cnd.and("group_id","like","%"+bean.getGroup_id()+"%");
        }
        
        if (bean.getTagid_list() !=null && Strings.isNotBlank(bean.getTagid_list().toString())) {
            cnd.and("tagid_list","like","%"+bean.getTagid_list()+"%");
        }
        
        if (bean.getQr_scene_str() !=null && Strings.isNotBlank(bean.getQr_scene_str().toString())) {
            cnd.and("qr_scene_str","like","%"+bean.getQr_scene_str()+"%");
        }
        
        if (bean.getLatitude() !=null && Strings.isNotBlank(bean.getLatitude().toString())) {
            cnd.and("latitude","like","%"+bean.getLatitude()+"%");
        }
        
        if (bean.getLongitude() !=null && Strings.isNotBlank(bean.getLongitude().toString())) {
            cnd.and("longitude","like","%"+bean.getLongitude()+"%");
        }
        
        if (bean.getPrecision() !=null && Strings.isNotBlank(bean.getPrecision().toString())) {
            cnd.and("precision","like","%"+bean.getPrecision()+"%");
        }
        
        if (bean.getSession_key() !=null && Strings.isNotBlank(bean.getSession_key().toString())) {
            cnd.and("session_key","like","%"+bean.getSession_key()+"%");
        }
        
        if (bean.getPassword() !=null && Strings.isNotBlank(bean.getPassword().toString())) {
            cnd.and("password","like","%"+bean.getPassword()+"%");
        }
        
        if (bean.getPoints() !=null && Strings.isNotBlank(bean.getPoints().toString())) {
            cnd.and("points","like","%"+bean.getPoints()+"%");
        }
        
        if (bean.getReceive_event() !=null && Strings.isNotBlank(bean.getReceive_event().toString())) {
            cnd.and("receive_event","like","%"+bean.getReceive_event()+"%");
        }
        
        return Result.data(wxUserService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增微信用户信息表")
    @ApiFormParams(
        implementation = Wx_authorizationsuser.class
    )
    @ApiResponses
    @SLog(tag = "新增微信用户信息表", value = "${args[0].id}")
    @SaCheckPermission("wxprogram.user.create")
    public Result<?> create(@Param("..")Wx_authorizationsuser wxUser, HttpServletRequest req) {
        wxUser.setCreatedBy(SecurityUtil.getUserId());
        wxUserService.insert(wxUser);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改微信用户信息表")
    @ApiFormParams(
        implementation = Wx_authorizationsuser.class
    )
    @ApiResponses
    @SLog(tag = "修改微信用户信息表", value = "${args[0].id}")
    @SaCheckPermission("wxprogram.user.update")
    public Result<?> update(@Param("..")Wx_authorizationsuser wxUser, HttpServletRequest req) {
        wxUser.setUpdatedBy(SecurityUtil.getUserId());
        wxUserService.updateIgnoreNull(wxUser);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取微信用户信息表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("wxprogram.user")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        Wx_authorizationsuser wxUser = wxUserService.fetch(id);
        if (wxUser == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxUser);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除微信用户信息表")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除微信用户信息表", value = "${args[0]}")
    @SaCheckPermission("wxprogram.user.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        Wx_authorizationsuser wxUser = wxUserService.fetch(id);
        if (wxUser == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxUserService.delete(id);
        req.setAttribute("_slog_msg", wxUser.getId());
        return Result.success();
    }
}
